//	ID : pallab81
// 	PROG : 459
// 	LANG : C++
// 	2011-07-06-16.56.27 Wednesday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define LN(X) (int)(X).length()

int grid[30][30];
int mx;
string line;
int vis[ 30 ];
string feed;
inline void print(){
    for(int i=0;i<=mx; ++i ){
        for(int j=0;j<=mx;++j){
            printf("%d",grid[i][j]);
        }
        printf("\n");
    }

}
inline void eatIt(){
    string ss;
    getline(cin,ss);
}
inline void read(){
    char ch;
    cin>>ch;
    eatIt();
    mx = ch-'A';
    while( true ){
        getline(cin,line);
        if( LN(line)<2 )break;
        grid[ line[0]-'A' ][ line[1]-'A' ]=1;
        grid[ line[1]-'A' ][ line[0]-'A' ]=1;
    }

}
inline void re(int now_node){
    if( vis[now_node] == -1 ){
        vis[ now_node ] = 1;
        for(int i=0; i<=mx; ++i ){
            if( grid[now_node][i] == 1 ){
                re(i);
            }
        }
    }
}
inline void proc(){
    int tot=0;
    //print();
    memset( vis, -1, sizeof(vis) );
    for(int i=0;i<=mx;++i ){
        if( vis[i]==-1 ){
            //cout<<"ready to go..\n";bip;
            ++tot;
            re(i);
        }
    }
    printf("%s",feed.c_str());
    printf("%d\n",tot);
}
inline void doit(){
    memset(grid,0,sizeof(grid));
    read();
    proc();
}

int main(){
    int cases;
    scanf("%d",&cases);
    while(cases--){
        doit();
        feed="\n";
    }

return 0;
}

